package cn.com.hatech.blog.module.admin.comment.controller;

import cn.com.hatech.blog.common.controller.BaseController;
import cn.com.hatech.blog.common.exception.TipException;
import cn.com.hatech.blog.common.result.RestResponse;
import cn.com.hatech.blog.module.admin.comment.entity.Comment;
import cn.com.hatech.blog.module.admin.comment.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.controller
 * @className: CommentController
 * @author: xuruikai
 * @description: 评论管理控制器
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@RestController
@RequestMapping("api/v1/admin/comments")
@Slf4j
public class CommentRestController extends BaseController {


    @Resource
    private ICommentService commentsService;

    /**
     * 删除一条评论
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponse delete(@RequestParam String id) {
        commentsService.deleteById(id);
        return RestResponse.ok();
    }

    @PostMapping(value = "/status")
    @ResponseBody
    @Transactional(rollbackFor = TipException.class)
    public RestResponse updateStatus(@RequestParam String id) {
        try {
            Comment comments = new Comment();
            comments.setId(id);
            comments.setStatus(1);
            commentsService.update(comments);
        } catch (Exception e) {
            String msg = "操作失败";
            if (e instanceof TipException) {
                msg = e.getMessage();
            } else {
                log.error(msg, e);
            }
            return RestResponse.fail(msg);
        }
        return RestResponse.ok();
    }
}
